import com.powerfl.frm.taf.*;

import flash.net.*;
import flash.utils.*;

private var _file:FileReference;

private function fileInit():void{
	if( SELF._file ){}else{
		SELF._file = new FileReference;
	}
}

final public function fileGet( $flow:String, ...$filters ):*{ //'Images,*.jpg;*.gif;*.png'
	var i:int, j:int, filter:Array, temp:Array;
	fileInit();
	SELF._file.addEventListener( Event.SELECT, function( $e:Event ):void{
		$e.target.removeEventListener( $e.type, arguments.callee ); 
		$e.target.load();
	} );
	if( $filters.length ){
		filter = [];
		for( i = 0, j = $filters.length ; i < j ; ++i ){
			temp = $filters[i].split(',');
			filter[filter.length] = new FileFilter( temp[0], temp[1]); 
		}
	}
	SELF._file.addEventListener( Event.COMPLETE, function( $e:Event ):void{
		$e.target.removeEventListener( $e.type, arguments.callee );
		flowCall( $flow, $e.target.name, $e.target.data );
	} );
	if( filter ){
		SELF._file.browse( filter );
	}else{
		SELF._file.browse();
	}
}
final public function fileSet( $data:ByteArray, $name:String ):void{
	fileInit();
	SELF._file.save( $data, $name );
}
final public function fileExtention( $data:ByteArray ):String{
	var val1:uint, val2:uint, val3:uint, val4:uint;
	$data.position = 0;
	val1 = $data.readUnsignedByte();
	val2 = $data.readUnsignedByte();
	val3 = $data.readUnsignedByte();
	val4 = $data.readUnsignedByte();
	if( val1 === 0xff && val2 === 0xd8 && val3 === 0xff ){
		return '.jpg';
	}else if( val1 === 0x89 && val2 === 0x50 && val3 === 0x4e ){
		return '.png';
	}else{
		return '';
	}
}


final public function pngEncode( img:BitmapData ):ByteArray{
	var i:int, j:int, k:uint, png:ByteArray, IHDR:ByteArray, IDAT:ByteArray;
	png = new ByteArray;
	png.writeUnsignedInt( 0x89504e47 );
	png.writeUnsignedInt( 0x0D0A1A0A );
	
	IHDR = new ByteArray;
	IHDR.writeInt( img.width );
	IHDR.writeInt( img.height );
	IHDR.writeUnsignedInt( 0x08060000 );
	IHDR.writeByte(0);
	
	pngChunk( png, 0x49484452, IHDR );
	
	IDAT = new ByteArray;
	for( i = 0 ;i < img.height ; ++i ){
		IDAT.writeByte(0);
		if( img.transparent ){
			for( j = 0 ; j < img.width ; ++j ){
				k = img.getPixel32( j, i );
				IDAT.writeUnsignedInt(uint( ((k&0xFFFFFF)<<8) | (k>>>24) ));
			}
		}else{
			for( j = 0 ; j < img.width ; ++j ){
				k = img.getPixel(j,i);
				IDAT.writeUnsignedInt(uint( ((k&0xFFFFFF)<<8) | 0xFF ));
			}
		}
	}
	IDAT.compress();
	
	pngChunk(png,0x49444154,IDAT);
	pngChunk(png,0x49454E44,null);
	
	pngTable = null;
	return png;
}

private var pngTable:Array;

private function pngChunk( png:ByteArray, type:uint, data:ByteArray ):void{
	var i:int, n:uint, c:uint, k:uint, len:uint, e:uint, p:uint;
	if ( SELF.pngTable ){}else{
		SELF.pngTable = [];
		for ( n = 0 ; n < 256 ; ++n ){
			c = n;
			for ( k = 0 ; k < 8; ++k ) {
				if (c & 1) { 
					c = uint(uint(0xedb88320) ^ uint(c >>> 1)); 
				}else{
					c = uint(c >>> 1);
				}
			}
			SELF.pngTable[n] = c;
		}
	}
	len = 0;
	if (data != null) {
		len = data.length;
	}
	png.writeUnsignedInt(len);
	p = png.position;
	png.writeUnsignedInt(type);
	if ( data != null ) {
		png.writeBytes(data);
	}
	e = png.position;
	png.position = p;
	c = 0xffffffff;
	for ( i = 0; i < ( e - p ) ; ++i ) {
		c = uint(SELF.pngTable[(c ^ png.readUnsignedByte())&uint(0xff)] ^ uint(c >>> 8));
	}
	c = uint(c^uint(0xffffffff));
	png.position = e;
	png.writeUnsignedInt(c);
}